<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\api\controller;

use think\response\Json;
use app\api\service\Cashier as CashierService;
use app\common\service\ExchangeService;
use app\common\model\Order as OrderModel;
use app\common\model\OrderAddress as OrderAddressModel;
use app\common\model\OrderGoods as OrderGoodsModel;
use app\common\model\Goods as GoodsModel;
use app\api\service\User as UserService;

/**
 * 订单付款控制器 (收银台)
 * Class Cashier
 * @package app\api\controller
 */
class Cashier extends Controller
{
    /**
     * 获取支付订单的信息
     * @param int $orderId 订单ID
     * @param string $client 指定的客户端
     * @return Json
     * @throws \cores\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function orderInfo(int $orderId, string $client): Json
    {
        $CashierService = new CashierService;
        $data = $CashierService->setOrderId($orderId)->setClient($client)->orderInfo();
        return $this->renderSuccess($data);
    }

    /**
     * 确认订单支付事件
     * @param int $orderId 订单ID
     * @param string $method 支付方式
     * @param string $client 指定的客户端
     * @param array $extra 附加数据
     * @return Json
     * @throws \cores\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function orderPay(int $orderId, string $method, string $client, array $extra = []): Json
    {
        $CashierService = new CashierService;
        $data = $CashierService->setOrderId($orderId)
            ->setMethod($method)
            ->setClient($client)
            ->orderPay($extra);

        // 通知积分平台-下单    
        $orderData=(new OrderModel)->where('order_id',$orderId)->find();// 订单信息
        $addressData=(new OrderAddressModel)->where('order_id',$orderId)->find();// 订单地址信息
        $user = UserService::getCurrentLoginUser(true);// 当前用户信息

        $jsonData['appid']=$user['appid'];
        $jsonData['shop_order_no']=$orderData['order_no'];
        $jsonData['app_user_id']=$user['app_user_id'];
        $jsonData['app_user_name']=$user['nick_name'];
        $jsonData['sent_to_user']=$addressData['name'];
        $jsonData['sent_to_address']=$addressData['detail'];
        $jsonData['sent_to_tel']=$addressData['phone'];
        $orderGoodsData=(new OrderGoodsModel)->where('order_id',$orderId)->select();// 订单商品信息
        foreach($orderGoodsData as $k => $goods){
             $goods_arr[$k]['goods_uuid']=(new GoodsModel)->where('goods_id',$goods['goods_id'])->value('goods_uuid');// goods_uuid
             $goods_arr[$k]['goods_sku_uuid']=0;//未完成
             $goods_arr[$k]['num']=$goods['total_num'];
        }
        $jsonData['goods_arr']=json_encode($goods_arr);
        $placeOrder=ExchangeService::placeOrder($jsonData);//去通知
       
        return $this->renderSuccess($data, $CashierService->getMessage() ?: '下单成功');
    }

    /**
     * 交易查询
     * @param string $outTradeNo 商户订单号
     * @param string $method 支付方式
     * @param string $client 指定的客户端
     * @return Json
     * @throws \cores\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function tradeQuery(string $outTradeNo, string $method, string $client): Json
    {
        $CashierService = new CashierService;
        $result = $CashierService->setMethod($method)->setClient($client)->tradeQuery($outTradeNo);
        $message = $result ? '恭喜您，订单已付款成功' : ($CashierService->getError() ?: '很抱歉，订单未支付，请重新发起');
        return $this->renderSuccess(['isPay' => $result], $message);
    }
}